﻿using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XsqTech.SmartOA.Service.Background
{
    public class TimedHostedService: BackgroundService
    {
        private readonly IServiceProvider _serviceProvider;
        private Timer _timer;   //定时器

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="serviceP"></param>
        public TimedHostedService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            //_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); //定时器创建后马上执行，每10秒钟执行一次

            return Task.CompletedTask;
        }
        /// <summary>
        /// 定时器具体执行的方法
        /// </summary>
        /// <param name="state"></param>
        private void DoWork(object state)
        {
            Console.WriteLine("fsnkfgjdbgfl");
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public override void Dispose()
        {
            _timer?.Dispose();  //如果_timer对象不为null，则销毁

            base.Dispose();
        }
    }
}
